
$(function () {
    $(".dialog").add('a[target=dialog]').click(function (e) {
        e.preventDefault();
        var title = $(this).attr('title') || $.trim($(this).text());
        var url = $(this).attr('href') || $(this).attr('src');
        var options = eval($(this).attr('options')) || {};
        Popup(title, url, options);
        return false;
    });

    $(".tabs").tabs();

    $(".tree")
    .bind("loaded.jstree", function (e, data) {
        $(this).jstree("open_all");
        $.jstree._reference(this).open_all();
    })
    .jstree({
        core: { "animation": 0 },
        plugins: ["themes", "html_data"]
    });

    $('form').submit(function () { if ($(this).valid()) { Progress(100); return true; } else { return false; } });
});

function Link(url) {
    window.location.href = url;
    return false;
}

function Open(url, features) {
    window.open(url, null, features, true);
    return false;
}

function Popup(title, url, options) {
    var divId = "dialog" + Math.round(Math.random() * 100);
    var maxwidth = $(window).width() - 60;
    var maxheight = $(window).height() - 60;
    if (!options)
        options = {};

    var width = options.width || maxwidth;
    var height = options.height || maxheight;

    $("body", top.document).append('<div id="' + divId + '" title="' + title + '" style="overflow:visible;"><div class="loading"></div></div>');
    $('#' + divId, top.document).dialog({
        autoOpen: false,
        title: title,
        width: width,
        height: height,
        maxWidth: maxwidth,
        maxHeight: maxheight,
        minWidth: 600,
        minHeight: 400,
        modal: true,
        bgiframe: true,
        show: "fade",
        hide: "fade",
        buttons: [
            { text: Local('Vit.Web', "OK"), click: function () { var form = $('iframe:first', $(this)).contents().find('form:first'); if (form.is('form')) { var btn = $('input:submit', form); if (btn.is('input')) { btn.trigger('click'); } else { form.submit(); } } else { $(this).dialog("close"); } } },
            { text: Local('Vit.Web', "Cancel"), click: function () { $(this).dialog("close"); } }
            ],
        open: function () {
            $("#" + divId).html('<iframe src="' + url + '" frameborder="0" width="100%" height="100%" scrolling="auto"></iframe>');
        },
        close: function () {
            $('#' + divId).remove();
            if (options.refreshOnClosed) {
                window.location = window.location.href;
            }
            return false;
        },
        resizeStop: function () {
        }
    });
    $('#' + divId, top.document).dialog("open");
}

function Submit(formid, url) {
    var form;
    if (formid) {
        form = $('#' + formid);
    }
    if (form) {
        if (url)
            form.attr('action', url);
        form.submit();
    }
    else {
        form.submit();
    }
    return false;
}

function ShowMessage(title, msg, msgType, callback) {
    var globalMsg = $('#global-msg');
    if ($(globalMsg).is('div') == false) {
        $('body').append('<div id="global-msg"></div>');
        globalMsg = $('#global-msg');
    }
    var cls = "info-img";
    if (msgType == "error")
        cls = "error-img";

    var bttns = [
            { text: Local('Vit.Web', "OK"), click: function () { $(this).dialog("close"); } }
        ];
    if (callback) {
        bttns = [
            { text: Local('Vit.Web', "OK"), click: function () { $(this).dialog("close"); callback(true); } },
            { text: Local('Vit.Web', "Cancel"), click: function () { $(this).dialog("close"); callback(false); } }
        ];
    }
    globalMsg.html('<table><tr><td class="' + cls + '"></td><td class="caption">' + title + '</td></tr><tr><td colspan="2" class="msg">' + msg + '</td></tr></table>');
    globalMsg.dialog({
        autoOpen: false,
        title: title,
        modal: true,
        bgiframe: false,
        show: "fade",
        hide: "fade",
        buttons: bttns
    });
    globalMsg.dialog("open");
}

function Alert(title, msg, msgType) {
    ShowMessage(title, msg, msgType, null);
}

function Confirm(title, msg, msgType, callback) {
    ShowMessage(title, msg, msgType, function (s) { if (s) { callback(); } });
}

function ModalMask(visible) {
    var globalMask = $('#global-mask');
    if ($(globalMask).is('div') == false) {
        $('body').append('<div id="global-mask" class="ui-widget-overlay" style="position:absolute;z-index:9999;top:0px;left:0px;width:100%;height:100%;display:none;"></div>');
        globalMask = $('#global-mask');
    }
    if (visible)
        globalMask.show();
    else
        globalMask.hide();
}

function Progress(value, nomask) {
    var progressbar = $('#progressbar');
    if ($(progressbar).is('div') == false) {
        $('body').append('<div id="progressbar" style="width:400px;position:absolute;z-index:10000"></div>');
        progressbar = $('#progressbar');
        progressbar.css('left', parseInt(($('body').width() - progressbar.width()) / 2)).css('top', parseInt(($('body').height() - progressbar.height()) / 2));
    }
    if (value <= 0) {
        if (!nomask) {
            ModalMask(false);
        }
        progressbar.progressbar("destroy");
    }
    else {
        if (!nomask) {
            ModalMask(true);
        }
        progressbar.progressbar({ value: value });
    }
}

function Delete(form, url) {
    if (form.indexOf('/') >= 0) {
        Confirm(Local('Vit.Web', 'Information'), $.validator.format(Local('Vit.Web', 'ConfirmDeleteRecords'), 1), null, function () {
            window.location = form;
        });
    }
    else {
        var records = $('#' + form + ' input:checkbox[id][checked]').length;
        if (records <= 0) {
            Alert(Local('Vit.Web', 'Information'), Local('Vit.Web', 'MustSelectOneItemOrMore'), null);
        }
        else {
            Confirm(Local('Vit.Web', 'Information'), $.validator.format(Local('Vit.Web', 'ConfirmDeleteRecords'), records), null, function () {
                Submit(form, url);
            });
        }
    }
}

